//
//  ErrorHistoryViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/9.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "ErrorHistoryViewController.h"
#import "CBaseTable.h"
#import "ErrorHistoryModel.h"
#import "ErrorHistoryView.h"
#import "DetailsViewController.h"
#import "NoDataView.h"
#import "MWPhotoBrowser.h"
#import <AFNetworking/AFNetworking.h>

@interface ErrorHistoryViewController()<MWPhotoBrowserDelegate>
@property (strong,nonatomic) UIVisualEffectView *visualEffectView;
@property (nonatomic, strong) UIView *noDataView;
//@property (nonatomic, strong) NSArray *mainData;
@property (nonatomic, strong) CBaseTable  *mainView;
@property (nonatomic, strong) NoDataView  *DataView;
@property (nonatomic) BOOL isNetwork;
@property(nonatomic,strong)NSMutableArray *photosAr;
@property (nonatomic, strong) AFNetworkReachabilityManager *manager;

@end
@implementation ErrorHistoryViewController
- (void)initView
{
    [super initView];
    self.navBar.hidden=NO;
    self.isNetwork=YES;
    __block typeof (self)blockSelf=self;
    
    self.manager = [AFNetworkReachabilityManager manager];
    [self.manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(status==AFNetworkReachabilityStatusNotReachable){
            
            blockSelf.isNetwork=NO;
        }else
        {
            blockSelf.isNetwork=YES;
            
        }
    }];
    [self setTitle:@"历史任务"];
    [self loadData];
    ErrorHistoryView *errorHistory =[[ErrorHistoryView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64-10)];
    [self.view addSubview:errorHistory];

#pragma mark 进入详情页
    [errorHistory setGoToNextDetail:^(ErrorHistoryModel* model) {
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            
            DetailsViewController *detailVc =[[DetailsViewController alloc]init];
            detailVc.warningUuid=model.E_uuid;
            detailVc.type=1;
            detailVc.state=model.E_state;
            [blockSelf pushController:detailVc];
        }
    }];

#pragma mark 播放语音

    _photosAr=[[NSMutableArray alloc]init];
#pragma mark 放大图片
    
    [errorHistory setBlowUpImg:^(ErrorHistoryModel* model,NSString* num) {
        
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            NSString*urlString =[CClass getCacheData:qnDomain];
            [blockSelf.photosAr removeAllObjects];
            NSString*imgStr=model.E_image;
            NSString *videoStr=model.E_video;
            
            if ((imgStr ==nil||[imgStr isEqualToString:@""])) {
                if (videoStr.length>0) {
#pragma mark 播放视频
                    
                    [blockSelf.photosAr addObject:[MWPhoto videoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,videoStr]]]];
                }
            }else
            {
                NSArray *array = [imgStr componentsSeparatedByString:@","];
                for (int i=0; i<array.count; i++) {
                    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,[array objectAtIndex:i]]];
                    [blockSelf.photosAr addObject:[MWPhoto photoWithURL:url]];
                    
                }
                if (videoStr.length>0) {
#pragma mark 播放视频
                    
                    [blockSelf.photosAr addObject:[MWPhoto videoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,videoStr]]]];
                }
            }
            
            
            MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:blockSelf];
            // Present
            [blockSelf.navigationController pushViewController:browser animated:YES];
            browser.displayActionButton = NO;
            browser.displayNavArrows = NO;
            browser.displaySelectionButtons = NO;
            browser.zoomPhotosToFill = YES;
            browser.alwaysShowControls = NO;
            browser.enableGrid = YES;
            browser.startOnGrid = NO;
            browser.autoPlayOnAppear = NO;
            
            [browser setCurrentPhotoIndex:[num integerValue]];
            
            UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
            [backView bk_whenTapped:^{
                [blockSelf.navigationController popViewControllerAnimated:YES];
            }];
            
            UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_backWhite"];
            UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, (40-icoBack.size.height)/2, icoBack.size.width, icoBack.size.height)];
            imgBack.image=icoBack;
            imgBack.userInteractionEnabled=YES;
            [backView addSubview:imgBack];
            
            UILabel *backTitle = [[UILabel alloc] init];
            backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 0, 50 - imgBack.frame.size.width,40);
            
            [backTitle setText:@"返回"];
            [backTitle setTextColor:[UIColor whiteColor]];
            [backTitle setFont:F(14)];
            [backView addSubview:backTitle];
            UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
            browser.navigationItem.leftBarButtonItem = leftBarButtonItem;
            
        }
        
        
    }];

 
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    
    return _photosAr.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photosAr.count) {
        return [_photosAr objectAtIndex:index];
    }
    return nil;
}

@end
